define([ "jquery","site_tip","mustache.min","comm","files/file","plugin/jquery.textarea","plugin/jquery.form.min","files/flashChecker","text!/static/page/file_detail.html","text!/static/page/folder_detail.html","plugin/qtip/jquery.qtip.min","css!/static/js/jquery/qtip/jquery.qtip.css"],
		function($,Site_tip,Mustache,comm,File,Jtextarea,Jform,FlashChecker,FileDetailTpl,FolderDetailTpl) {
			//console.log(new Files());
			var fls= new FlashChecker();
			var site_tip = new Site_tip();
	//require(["files/main"],function(){});

	function File_detail() {
		this.file = null;
		this.isFobbin = false;
		this.isShow = false;
		this.type = 0;
		this.remark = '添加备注';//备注
		this.showPjt = false;//显示当前文件的项目个数
		this.selChkLen = 0;
	}
	
	File_detail.prototype.init = function(uuid, type) {
		this.type = arguments[1] || 0;
		this.file = new File();
		this.file.uuid = uuid;
		this.isFobbin = false;
		
		this.showUI(true);
		this.loadData();
	}
	
	File_detail.prototype.bindUI = function() {
		var line_num = 0;
		var oldstr = "";
		$("#f_dec").off();
		$("#f_dec").on("input propertychange",function(e){
			var str = $(this).val();
			if(str.length > 80){
				$(this).val($(this).val().substr(0,80));
				return false;
			}
			var selectionStart = $(this).getCursorPosition();
			var posStr = $(this).val().substr(0,selectionStart);
			var posArr = posStr.split("\n");
			line_num = posArr.length - 1;
			if(line_num <0){
				line_num = 0;
			}
			
			var rightStr = $(this).val().substr(selectionStart,$(this).val().length);
			var rightArr = rightStr.split("\n");
			var right_lN = rightArr.length - 1;
			
			if(line_num + right_lN < 3){
				return;
			}
			
			var index = line_num;
			var t_index = line_num -1;
			if(t_index <0 ){
				t_index = 0;
			}
			if(line_num == 0){
				line_num = 1;
			}
			var maxlen = line_num *16;
			if(right_lN == 0 && line_num  < $(this).attr('rows')-1){
				maxlen = 80;
			}
			if(t_index *16 +posArr[index].length > maxlen){
				var off = t_index *16 +posArr[index].length - maxlen;
				var substr = $(this).val().substr(0,selectionStart- off);
				$(this).val(substr+rightStr);
				$(this).setSelection(selectionStart,selectionStart)
			}

		});
		$("#f_dec").on("keydown",function(e){
			var oEvent = window.event ? e.keyCode : e.which;
			if (oEvent == 13) {
				var arr = $(this).val().split("\n");
				line_num = arr.length;
				if(line_num >= $(this).attr('rows')){
					return false;
				}
			}
			
		});
	}
	var getDetailProcess;
	File_detail.prototype.loadData = function() {
        var that = this;
        if(getDetailProcess)
        {
            getDetailProcess.abort();
        }
		if(that.showPjt){
			this.loadCountTpl();
			return;
		}


		that.loadEmptyTpl();
		var url = "/files/get_detail/"+that.type+"/"+that.file.uuid;
        getDetailProcess = $.getJSON(url, function(jsonData) {
			if (jsonData.success) {
				var life_span = jsonData.data.life_span;
				var auto_del = jsonData.data.delete_after_expired;
				var only_one = jsonData.data.open_counter;
				var f_pwd = jsonData.data.f_pwd;
				that.isFobbin = !jsonData.data.isOpen;
				var view = {
					"name" : jsonData.data.f_name,
					"ext_name" : jsonData.data.f_ext_name,
					"size" : jsonData.data.f_size,
					"life_span" : life_span,
					"f_pwd" : f_pwd,
					"auto_del" : auto_del,
					"only_one" : only_one,
					"f_dec" : jsonData.data.f_dec,
					"f_id" : jsonData.data.f_id,
					"f_type" : jsonData.data.f_type,
					"f_count" : jsonData.data.f_count,
					"remain_viewed" : jsonData.data.remain_viewed,
					"open_counter" : jsonData.data.open_counter,
				};
				that.loadTpl.apply(that, [view]);
			}
			else
			{
				//$("#fileInfo .detail-loading").remove();
				site_tip.setContent(jsonData.message);
				if(jsonData.error_code==7100 || jsonData.error_code==7001)
				{
					require(["folder/main"],function(Folder){
						new Folder().reload();
					});
				}
			}
		});
		return false;
	}
	File_detail.prototype.loadEmptyTpl = function()
	{
        if($("#fileInfo").find(".detail-loading").length<=0)
        {
            $("#fileInfo").prepend("<div class='detail-loading'></div>");
        }

	}
	File_detail.prototype.loadTpl = function(view) {
		var that = this;
		var tmpl = FileDetailTpl;
		if(view.f_type == 1 || that.showPjt){
			tmpl = FolderDetailTpl;
		}
		/*$("#fileInfo").children().hide(function(){

		},200);*/
		$("#fileInfo").remove();
		var item_text = Mustache.to_html(tmpl, view);
		$('#mainView').append(item_text);
		that.initUI.apply(that, [$("#fileInfo"),view]);
		that.initPolicy_html.apply(that,[view]);
		//$("#fileInfo").height($("#leftPanel").height());
		$("#fileInfo").fadeIn();
		return false;
		
	}
	
	/**
	 * 显示文件列表包含行数
	 */
	File_detail.prototype.loadCountTpl = function() {
		var pid = $("#wfo").data("last_pid");
		var type = $("#wfo").data("type");
		var that = this;
		$("#fileInfo").remove();
		var is_all = false;
		var total = parseInt($("#fileTotal").val());
		if(that.selChkLen == total || that.selChkLen == 0){
			is_all = true;
		}else{
			total = that.selChkLen;
		}
		total = total||0;
		var view = {"f_count":total};
		if(!is_all){
			view.no_all = true;
		}
		if($('#search-detail:visible .total').size() != 0){
			var searchSize = $('#search-detail .total').text();
			if(searchSize != ''){
				view.f_count = searchSize;
			}
		}
		var item_text = Mustache.to_html(FolderDetailTpl, view);
		$('#mainView').append(item_text);
		$("#fileInfo").find(".authority").hide();
		$("#fileInfo").find(".project-num").show();
		$("#fileInfo").find(".remark").hide();
		//$("#fileInfo").height($("#leftPanel").height()-$('#mainPanel').height());
		$("#fileInfo").show();
		/*$.getJSON("/files/getAllTotalByPid",{pid:pid,type:type},function(jsonData){
			if(jsonData.ret == -1){

				return false;
			}
		});*/
		return false;
	}

	File_detail.prototype.showUI = function(flag) {
		this.isShow = flag;
		if (flag) {
			$("#fileInfo").show();
			return;
		}
		$("#fileInfo").hide();
	}
	
	
	File_detail.prototype.initUI = function(item,view){
		var that = this;
		if(view.f_dec == null || view.f_dec == "" || view.f_dec == undefined){
			$("#f_dec").val(that.remark);
			$("#f_dec").css("text-decoration","underline");
		}
		item.off();
		item.on("click",function(e){
			comm.stopEvent(e);
		});
		
		this.bindUI();
		var isEdit = false;
		item.on("blur",".t_area",function (e){
			comm.stopEvent(e);
			isEdit = false;
			$(this).css("background-color","transparent");
			$(this).css("border","solid 1px #f3f3f3;");
			$(this).css("color","#939292");
			
			var arr = $(this).val().split("\n");
			var hasValue = false;
			for(var i= 0;i<arr.length;i++){
				if(arr[i] != "" && arr[i] != " "){
					hasValue = true;
					break;
				}
			}
			var me = $(this);
			//console.log($(this)[0].defaultValue);
			if(me.val() != that.remark && me.val()!=me[0].defaultValue){
				$(this).css("text-decoration","none");
                //item.find(".file-comment").ajaxSubmit();
                //item.find(".file-comment").ajaxSubmit();

                var options = item.find(".file-comment");

                //解决ie8及以下的版本问题
                /*if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/8./i)=="8."){
                    var method = options.type || options.attr2('method');
                    var action = options.url  || options.attr2('action');

                    var url = (typeof action === 'string') ? $.trim(action) : '';
                    url = url || window.location.href || '';
                    if (url) {
                        url = (url.match(/^([^#]+)/)||[])[1];
                    }

                    //post提交


                } else {
                    options.ajaxSubmit();
                }*/
				site_tip.loadingMsg("正在保存备注信息...");
				//site_tip.setContent("正在修改！",false,false);
				$.post("/files/update_detail", options.serializeArray(),function(ret){
					if(ret.success)
					{
						site_tip.setContent("备注信息保存成功！",false,false);
						me[0].defaultValue = me.val();
					}
					else
					{
						site_tip.setContent(ret.messsage,false,false);
					}

				},'json');
            }
			if(me.val() == "" || me.val() == " " || hasValue == false){
				me.val(that.remark);
				me.css("text-decoration","underline");
			}
				
		});
		item.on("focus",".t_area",function (e){
			comm.stopEvent(e);
			$(this).css("text-decoration","none");
			if($(this).val() == that.remark){
				$(this).val("");
			}
			
				
		});
		
		item.on("click",".t_area",function (e){	
			$(this).css("border","solid 1px #aaaaaa");
			$(this).css("color","#000000");
			$(this).css("background-color","#ffffff");
			isEdit = true;
			
		});
		
		item.on("mouseenter",".t_area",function (e){
			if(isEdit == true){
				return;
			}
			$(this).css("border","solid 1px #e7e7e7");
			$(this).css("color","#939292");
		});
		item.on("mouseleave",".t_area",function (e){
			if(isEdit == true){
				return;
			}
			$(this).css("border","solid 1px #f3f3f3;");
			$(this).css("color","#939292");
		});
		
		var copy = $("#fileInfo").find(".icon-copy");

		if(copy.length != 0){
			if(!fls.f || fls.v < 10 ){
				/*if(comm.isIE()){
				 that.ieClipboard(sl_uuid);
				 return;
				 }
				 okBtn.addClass("unavailable");
				 okBtn.text("");
				 $("#copy_share").find(".err").css("display","block");

				 return;*/
				copy.qtip({
					content: '不能自动复制，请使用“Ctrl+C”或者右键“复制”查阅密码到剪贴板',
					position:{
						at: 'top center',
						my:'bottom center'
					},
					show: {
						event:'click',
						effect: false
					},
					hide: {
						event:'unfocus',
						effect: false
					}
				});
			}
			else{
				var clipUrl = "clipboard";
				if(comm.lessThenIE(9) && comm.isIE()){
					clipUrl = "clipboardLess";
				}

				require([clipUrl],function(ZeroClipboard){
					var client = new ZeroClipboard(copy);
					client.on('ready', function(readyEvent ) {
						var result = $("#set_policies").find(".copy_result");
						client.on('aftercopy', function(event, args) {
							copy.qtip({
								style:{
									classes:"qtip-blue"
								},
								content: '复制成功',
								position:{
									at: 'top center',
									my:'bottom center'
								},
								show: {
									event: false,
									ready: true,
									effect: false
								},
								hide: {
									event:'unfocus',
									effect: false
								}
							});
						});
					});
				});
			}
			/*copy.off("click").on("click",function(e){
				comm.stopEvent(e);
			});*/

		}
	}

	/**
	 * 根据访问权限选择显示内容
	 */
	File_detail.prototype.initPolicy_html = function(view) {
		if (this.isFobbin) {
			$("#op_policies").find(".fobbin_policy").css("display", "block");
			$("#op_policies").find(".has_policy").css("display", "none");
		} else {
			$("#op_policies").find(".fobbin_policy").css("display", "none");
			$("#op_policies").find(".has_policy").css("display", "block");

			var content = $("#op_policies").find(".content");

			if (view == null) {
				content.find("p:eq(0)").css("display", "none");
				content.find("p:eq(1)").css("display", "none");
				content.find("p:eq(2)").css("display", "none");
				content.find("p:eq(3)").css("display", "none");
				content.find("p:eq(4)").css("display", "none");
				return;
			}

			if (view.life_span != 0 && view.life_span != undefined) {
				content.find("p:eq(0)").css("display", "block");
			} else {
				content.find("p:eq(0)").css("display", "none");
			}

			if (view.only_one > 0) {
				content.find(".content").find("p:eq(1)")
						.css("display", "block");
				content.find(".content").find("p:eq(2)")
					.css("display", "block");
			} else {
				content.find("p:eq(1)").css("display", "none");
				content.find("p:eq(2)").css("display", "none");
			}

			if (view.auto_del == 1) {
				content.find("p:eq(3)").css("display", "block");
			} else {
				content.find("p:eq(3)").css("display", "none");
			}
			
			if (view.f_pwd != 0 && view.f_pwd != undefined) {
				content.find("p:eq(4)").show();
			} else {
				content.find("p:eq(4)").hide();
			}
		}
		
		/*$('#fileInfo').mCustomScrollbar({
			theme:"minimal"
		});*/

	}
	
	File_detail.prototype.destroy = function() {
		this.file = null;
		this.isFobbin = false;
		this.isShow = false;
	}
	
	return File_detail;

});